#pragma warning disable CS0168     // Class 'xxx' is never instantiated
#pragma warning disable CS0649    // Auto-property accessor 'xxx.set' is never used
#pragma warning disable CS0414

using Nett;
using Newtonsoft.Json;

namespace ConfigToml;

/// <summary>
/// 重写ToString, 以Json字符串形式打印对象
/// </summary>
public abstract class JsonPrinter
{
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this, new JsonSerializerSettings { Formatting = Formatting.Indented });
    }
}

/// <summary>
/// 系统配置
/// </summary>
public class SystemSection : JsonPrinter
{
    public string Domain { get; set; } // 官方域名
    public int Official { get; set; } // 官方账号
    public bool Monitor { get; set; } // 开启监控
    public List<int> Robots { get; set; } // 机器人账号
    public Dictionary<string, string> Nodes { get; set; } // 服务器节点
}

/// <summary>
/// 微服务配置
/// </summary>
public class ServiceSection : JsonPrinter
{
    public string Host { get; set; }
    public int Port { get; set; }
    public string Token { get; set; }
    public string Mark { get; set; }
}

/// <summary>
/// 日志配置
/// </summary>
public class LogSection: JsonPrinter
{
    /// <summary>
    /// 日志文件
    /// </summary>
    public class LogFileSection : JsonPrinter
    {
        public string Path { get; set; } // 日志输出路径
        public int Rotation { get; set; } // 文件切割周期(天)
        public int Monitor { get; set; } // 文件监控周期(秒)
        public int Retain { get; set; } // 日志保留时间(天)
        public int Limit { get; set; } // 单个文件大小上限(M)
    }

    /// <summary>
    /// ES配置
    /// </summary>
    public class ElasticSection : JsonPrinter
    {
        public List<string> Urls { get; set; } // ES节点URL列表
        public string Account { get; set; } // 账户名
        public string Password { get; set; } // 密码
        public string Index { get; set; } // 索引名称
    }
    
    public string Level { get; set; }
    public bool Console { get; set; }
    public LogFileSection File { get; set; }
    public ElasticSection Elastic { get; set; }
}


/// <summary>
/// 数据存储集
/// </summary>
public class RepoSection : JsonPrinter
{
    /// <summary>
    /// 数据库
    /// </summary>
    public class Database : JsonPrinter
    {
        public string Host { get; set; }
        public int Port { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public string Schema { get; set; }
        public string Charset { get; set; }
    }

    /// <summary>
    /// 管理系统数据库
    /// </summary>
    public Database Admin { get; set; }

    public class LogicRepo : JsonPrinter
    {
        public Database Master { get; set; }
        public Database[] Slave { get; set; }
    }

    /// <summary>
    /// 逻辑业务数据库(集群版)
    /// </summary>
    public LogicRepo Logic { get; set; }
}

public class RedisSection : JsonPrinter
{
    public string Host { get; set; }
    public int Port { get; set; }
    public string Auth { get; set; }
}

/// <summary>
/// Kafka配置
/// </summary>
public class KafkaSection : JsonPrinter
{
    public List<string> Hosts { get; set; } // Kafka节点地址列表
    public string Account { get; set; } // 账户名
    public string Password { get; set; } // 密码
    public string ProducerTopic { get; set; } // 生产者主题
    public string ConsumerTopic { get; set; } // 消费者主题
    public string Group { get; set; } // 消费者组
    public bool SaslEnable { get; set; } // 是否启用SASL认证
}

/// <summary>
/// 全局App配置
/// </summary>
public class GlobalSection : JsonPrinter
{
    public string Name { get; set; } // 应用名称
    public string Model { get; set; } // 模式
    public string Lang { get; set; } // 语言环境
    public string Version { get; set; } // 版本号

    public SystemSection System { get; set; } // 系统配置
    public Dictionary<string, ServiceSection> Service { get; set; } // 外接服务
    public LogSection Log { get; set; } // 文件日志配置
    public RepoSection Repo { get; set; } // 数据库配置
    public RedisSection Redis { get; set; } // Redis配置
    public KafkaSection Kafka { get; set; } // Kafka配置
}

/// <summary>
/// 冗余设计，方便精简化读取
/// </summary>
public class Config
{
    private Config(){ }

    public static GlobalSection Global { get; set; }

    public static string Name { get; set; } // 应用名称
    public static string Model { get; set; } // 模式
    public static string Lang { get; set; } // 语言环境
    public static string Version { get; set; } // 版本号
    public static SystemSection System { get; set; } // 系统配置
    public static Dictionary<string, ServiceSection> Service { get; set; } // 外接服务
    public static LogSection Log { get; set; } // 文件日志配置
    public static RepoSection Repo{ get; set; } // 数据库配置
    public static RedisSection Redis { get; set; } // Redis配置
    public static KafkaSection Kafka { get; set; } // Kafka配置

    /// <summary>
    /// 初始化配置文件
    /// </summary>
    /// <param name="path"></param>
    public static void Init(string path)
    {
        Global ??= Toml.ReadFile<GlobalSection>(path);
        Name = Global.Name;
        Model = Global.Model;
        Lang = Global.Lang;
        Version = Global.Version;
        System = Global.System;
        Service = Global.Service;
        Log = Global.Log;
        Repo = Global.Repo;
        Redis = Global.Redis;
        Kafka = Global.Kafka;
    }
}